/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var threeSum = function (nums) {

    let arr = nums.slice()
    arr.sort((a, b) => a - b)
    let len = arr.length
    let rslt = []
    let p, q, r
    for (let i = 0; i < len - 1; i++) {
        p = arr[i]
        q = i + 1
        r = len - 1

        while (q < r) {
            if (p + arr[q] + arr[r] === 0) {
                // 去重
                let val = [p, arr[q], arr[r]]
                let key = val.toString()
                if (!rslt[key]) {
                    rslt[key] = val
                    rslt.push(val)
                }
                q++
            }
            if (p + (arr[q] + arr[r]) < 0) {
                q++
            } else {
                r--
            }
        }
    }
    return rslt.slice()
};

console.log(threeSum([-2, 0, 1, 1, 2]))